package com.common.frame.extension

import android.graphics.Color
import android.text.SpannedString
import android.text.method.LinkMovementMethod
import android.view.View
import android.widget.TextView

fun View.setSelected(resId: Int) {
    isSelected = id == resId
}

fun View?.setSingleClick(ignoreTime: Int = 1000, onclick: (View) -> Unit) {
    var mLastClickMills = 0L
    this?.setOnClickListener {
        try {
            val currentTimeMillis = System.currentTimeMillis()
            if (currentTimeMillis - mLastClickMills >= ignoreTime) {
                mLastClickMills = currentTimeMillis
                onclick.invoke(it)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}


fun MutableList<View>.setSingleClick(ignoreTime: Int = 1000, onclick: (View) -> Unit) {
    forEach {
        it.setSingleClick(ignoreTime, onclick)
    }

}

/**
 * 获取控件的中心位置（整个屏幕）
 */
fun View.getCenterLocation(): IntArray {
    val location = intArrayOf(0, 0)
    getLocationOnScreen(location)

    val endPosition = intArrayOf(0, 0)
    endPosition[0] = location[0] + width / 2
    endPosition[1] = location[1] + height / 2
    return endPosition

}

fun TextView.setSpannedString(spanned: SpannedString) {
    movementMethod = LinkMovementMethod.getInstance()
    highlightColor = Color.TRANSPARENT
    text = spanned
}